import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/http/models/appointmentListModel.dart';

class MakeBloc implements BlocBase {

  bool isFirstLoading = true;
  int page = 0;
  final int limit = 10;
  List<AppointmentListModel> list = [];

  final List<AppointmentListModel> defaultData = [];

  MakeBloc() {
    getMake();
  }

  // ##########  DEFAULT  ##############

  final List<AppointmentListModel> makeDefault = [];

  // ##########  STREAMS  ##############

  /// 预约stream
  StreamController<List<AppointmentListModel>> _make = StreamController.broadcast();
  Sink<List<AppointmentListModel>> get _makeSink => _make.sink;
  Stream<List<AppointmentListModel>> get makeStream => _make.stream;

  void dispose(){
    _make.close();
  }

  // ##########  HANDLING  ##############

  /// 获取预约列表数据
  void getMake() async {
    page += 1;
    List<AppointmentListModel> result = await UserApis.appointmentList(page: page, limit: limit);
    isFirstLoading = false;
    if (result.isNotEmpty) {
      list.addAll(result);
      _makeSink.add(list);
    } else {
      if (!isFirstLoading) showToast("已经没有了喔~ww");
      _makeSink.add(list);
    }
  }

  /// 刷新预约列表
  void onRefresh() async {
    page = 0;
    list = [];
    getMake();
  }
}